<template>
    <div id="GroupManage" class="center_div">
        <div class=" leftTreeClass tree_class dataManage18px">
            <div style="height: 90%;overflow:auto;margin-top: 2%">
                <el-tree check-strictly
                         :load="loadChildrenn"
                         highlight-current
                         lazy
                         node-key="id"
                         :props="defaultProps"
                         :filter-node-method="filterNode"
                         ref="intercomtree"
                         @node-click="loadDataaa">
                <span class="custom-tree-node dataManageBtn24px" slot-scope="{ node, data }">
                    <span v-if="data.type===0"><img class="treeLableImg" src="@/assets/images/mapicon/tree_fleet.png"
                    >{{ node.label }}</span>
                    <span v-if="data.type===1"><img src="@/assets/images/mapicon/tree_fleet.png"
                                                    class="treeLableImg">{{ node.label }}</span>
                </span>
                </el-tree>
            </div>
        </div>
        <div style="float:right;width: 80%">
            <div style="margin-top: 1.5%;" id="searchFormId" class="searchFormDataManageBtn22px">
                <el-form :inline="true" :model="formInline" class="demo-form-inline">
                    <el-form-item label="群组名称">
                        <el-input v-model="name" placeholder="群组名称"></el-input>
                    </el-form-item>
                    <el-form-item label="群组号码">
                        <el-input v-model="number" type="number" :min="0"
                                  onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))"></el-input>
                    </el-form-item>
                    <el-form-item label="分机号码">
                        <el-input v-model="extNumber" type="number" :min="0"
                                  onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))"></el-input>
                    </el-form-item>
                    <el-form-item label="所属部门">
                        <el-select v-model="fleetName" :placeholder="'请选择'" filterable clearable>
                            <el-option :value="fleetName" style="height: auto">
                                <el-tree
                                        :load="loadChildrenn"
                                        highlight-current
                                        lazy
                                        node-key="id"
                                        :props="defaultProps"
                                        :filter-node-method="filterNode"
                                        ref="tree"
                                        @node-click="loadData">
                            <span class="custom-tree-node dataManage18px" slot-scope="{ node, data }">
                                <span v-if="data.type===0"><img src="@/assets/images/mapicon/tree_fleet.png"
                                                                class="treeLableImg">{{ node.label }}</span>
                                <span v-if="data.type===1"><img src="@/assets/images/mapicon/tree_fleet.png"
                                                                class="treeLableImg">{{ node.label }}</span>
                                <span v-if="data.type===2"><img src="@/assets/images/mapicon/tree_sub.png"
                                                                class="treeLableImg">{{ node.label }}</span>
                            </span>
                                </el-tree>
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="系统类型">
                        <el-select v-model="systemId" :placeholder="'请选择'" filterable clearable>
                            <el-option
                                    v-for="item in systemOptionsAll"
                                    :key="item.System_ID"
                                    :label="item.System_alias"
                                    :value="item.System_ID">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button style="cursor: pointer" @click="querySearch" type="primary"
                                   class="dataManageBtn22BtnpPx">查询
                        </el-button>
                        <el-button style="cursor: pointer" @click="addGroup" type="primary"
                                   class="dataManageBtn22BtnpPx">新增
                        </el-button>
                    </el-form-item>
                </el-form>
            </div>
            <div class="table-wrap" style="width: 98%;margin-top: -1%">
                <el-table
                        :data="tableData"
                        style="width:100%;text-align: center;border-left:1px solid #EBEBEB;"
                        border
                        :header-cell-style="headerCellClassName"
                        cell-class-name="dataManage18px">
                    <el-table-column
                            prop="name"
                            align="center"
                            label="群组名称" :show-overflow-tooltip="true">
                    </el-table-column>
                    <el-table-column
                            prop="number"
                            align="center"
                            label="群组号码" :show-overflow-tooltip="true">
                    </el-table-column>
                    <el-table-column
                            prop="sipNumber"
                            align="center"
                            label="分机号码" :show-overflow-tooltip="true">
                    </el-table-column>
                    <el-table-column
                            prop="priority"
                            align="center"
                            label="群组优先级" :show-overflow-tooltip="true">
                    </el-table-column>
                    <el-table-column
                            prop="subNum"
                            align="center"
                            label="成员数量" :show-overflow-tooltip="true">
                    </el-table-column>
                    <el-table-column
                            align="center"
                            prop="freeReleaseTime"
                            label="空闲释放时长(s)" :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <span>{{scope.row.freeReleaseTime}}s</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                            class="align-right"
                            width="320"
                            header-align="center"
                            label="操作"
                            align="center">
                        <template slot-scope="scope">
                            <el-button @click="deleteGroup(scope.$index, tableData)" type="text" size="medium"
                                       class="dataManageBtn22px">删除
                            </el-button>
                            <el-button @click="modifyGroup(scope.$index, tableData)" type="text" size="medium"
                                       class="dataManageBtn22px">修改
                            </el-button>
                            <el-button @click="memberManger(scope.row)" type="text" size="medium"
                                       class="dataManageBtn22px">成员管理
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination
                        layout="total,prev, pager, next"
                        background
                        @current-change="changePageNum"
                        :current-page="pageNum"
                        :total="total">
                </el-pagination>
            </div>

        </div>
        <div>
            <el-dialog :title="title"
                       @close="closeDialog"
                       :visible.sync="showDialog"
                       width="50%"
                       append-to-body>
                <el-form ref="form" :model="form" :rules="rules" label-width="160px">
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="群组名称" prop="name">
                                <el-input v-model="form.name" clearable></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="群组号码" prop="number">
                                <!--                                <el-input v-model="form.number" type="number" clearable></el-input>-->
                                <el-input v-model="form.number" clearable type="number" :min="0"
                                          onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="空闲释放时长" prop="freeReleaseTime">
                                <!--                                <el-input v-model="form.freeReleaseTime" clearable></el-input>-->
                                <el-input v-model="form.freeReleaseTime" clearable type="number" :min="0"
                                          onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="优先级" prop="priority">
                                <el-select v-model="form.priority" placeholder="请选择">
                                    <el-option
                                            v-for="item in options"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="系统类型" prop="System_ID">
                                <el-select v-model="form.System_ID" placeholder="请选择">
                                    <el-option
                                            v-for="item in systemOptions"
                                            :key="item.System_ID"
                                            :label="item.System_alias"
                                            :value="item.System_ID">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="部门名称" prop="fleetName">
                                <el-select v-model="form.fleetName" :placeholder="'请选择'"
                                           :disabled="form.type==1?true:false" filterable>
                                    <el-option :value="form.fleetName" style="height: auto">
                                        <el-tree
                                                :load="loadChildrenBumen"
                                                highlight-current
                                                lazy
                                                node-key="id"
                                                class="dataManage18px"
                                                :props="defaultProps"
                                                :filter-node-method="filterNode"
                                                ref="tree"
                                                @node-click="loadDataBumen">
                                              <span class="custom-tree-node dataManage18px"
                                                    slot-scope="{ node, data }">
                                            <span v-if="data.type===0"><img src="@/assets/images/mapicon/tree_fleet.png"
                                                                            class="treeLableImg"
                                            >{{ node.label }}</span>
                                            <span v-if="data.type===1"><img src="@/assets/images/mapicon/tree_fleet.png"
                                                                            class="treeLableImg">{{ node.label }}</span>
                                        </span>
                                        </el-tree>
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="IP分机号码" prop="extNumber">
                                <el-input v-model="form.extNumber" type="number"
                                          onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="showDialog = false" class="dataManageBtn22BtnpPx">取 消</el-button>
                    <el-button type="primary" @click="dialogComfirm('form')" size="medium"
                               class="dataManageBtn22BtnpPx">确 定
                    </el-button>
                </div>
            </el-dialog>
        </div>


        <!----添加成员---->

        <el-dialog v-if="loadTree" width="70%" :title="title" :visible.sync="addShow" @close="dialogClose"
                   append-to-body>
            <div class="groupMgStyleBigBoxLeft">
                <div style="height:99%;width:99%;margin-top:1%;background:white;overflow:auto">
                    <el-tree check-strictly
                             :load="loadChildren"
                             show-checkbox
                             highlight-current
                             lazy
                             node-key="id"
                             :props="defaultProps"
                             :filter-node-method="filterNode"
                             ref="tree"
                             @check-change="handleCheckChange">
                            <span class="custom-tree-node dataManage18px" slot-scope="{ node, data }">
                                <span v-if="data.type===0"><img src="@/assets/images/mapicon/tree_fleet.png"
                                                                class="treeLableImg">{{ node.label }}</span>
                                <span v-if="data.type===1"><img src="@/assets/images/mapicon/tree_fleet.png"
                                                                class="treeLableImg">{{ node.label }}</span>
                                <span v-if="data.type===2"><img src="@/assets/images/mapicon/tree_sub.png"
                                                                class="treeLableImg">{{ node.label }}</span>
                            </span>
                    </el-tree>
                </div>
            </div>
            <div class="groupMgStyleBigBox">
                <el-table
                        :data="memberTableData"
                        style="width:100%;text-align: center;"
                        border
                        class="groupMgTableClass"
                        cell-class-name="memberMgdataManageText"
                        :header-cell-style="headerCellClassName">
                    <el-table-column align="center" label="名称" prop="name" :show-overflow-tooltip="true">
                    </el-table-column>
                    <el-table-column align="center" label="号码" prop="subNumber" :show-overflow-tooltip="true">
                    </el-table-column>
                    <el-table-column align="center" label="成员优先级" :show-overflow-tooltip="true">
                        <template v-slot="{row}">
                            <template>
                                <el-select v-model="row.priority" style="width: 120px">
                                    <el-option
                                            v-for="item in memberPriority"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value">
                                    </el-option>
                                </el-select>
                            </template>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="说话时长" :show-overflow-tooltip="true">
                        <template v-slot="{row}">
                            <template>
                                <el-input v-model="row.time" class="edit-input"/>
                            </template>
                        </template>
                    </el-table-column>
                    <template>
                        <el-table-column
                                class="align-right"
                                align="center"
                                label="操作">
                            <template v-slot="scope">
                                <!--<span><img src="@/assets/images/newIcon/sortUp.png"
                                           class="groupMgStyle"
                                           @click="sortUpUp(index)">
                                </span>-->
                                <span>
                                        <el-button @click="deleteMember(scope.row.subId)" type="text"
                                                   class="el-button el-button--text  memberMgdataManageBtn">删除</el-button></span>
                            </template>
                        </el-table-column>
                    </template>
                </el-table>
                <el-pagination
                        v-if="false"
                        class="dataManage18px"
                        layout="total,prev, pager, next"
                        background
                        @current-change="changesAddPageNum"
                        :current-page="addPage.pageNum"
                        :page-size="addPage.pageSize"
                        :total="memberTableData.length">
                </el-pagination>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addShow = false" class="dataManageBtn22BtnpPx">取 消</el-button>
                <el-button type="primary" @click="addGroupMember" class="dataManageBtn22BtnpPx">确 定
                </el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
  import IntercomGroupManage from '@/api/dataManages/IntercomGroupManage';
  import UserManage from '@/api/dataManages/UserManage';
  import SystemAddressBookManage from '@/api/dataManages/SystemAddressBookManage';

  export default {
    name: 'IntercomGroupManage',
    props: {
      FleetId: Number,
      userId: Number,
      UserAccount: String,
    },
    data() {
      return {
        groupSearchName: '',
        groupSearchNumber: '',
        groupSearchPriority: '',
        name: '',
        number: '',
        extNumber: '',
        priority: '',
        formInline: {},
        tableData: [],
        total: 0,
        pageNum: 1,
        pageSize: 8,
        addPage: {
          total: 0,
          pageNum: 1,
          pageSize: 4,
        },
        groupId: '',
        namem: '',
        form: {
          name: '',
          groupId: 0,
          type: 0, // 0-添加 1-修改
          number: '',
          priority: '',
          freeReleaseTime: 60,
          System_ID: '',
          fleetName: '',
          extNumber: '',
        },
        memberTableTitle: [
          {label: '成员', prop: 'subName'},
          {label: '归属部门', prop: 'fleetName'},
          {label: '编组', prop: 'groupName'},
        ],
        rules: {
          name: [
            {required: true, message: '请输入群组名称', trigger: 'blur'},
            {min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur'},
          ], number: [
            {required: true, message: '请输入群组号码', trigger: 'blur'},
            {min: 1, max: 16, message: '长度不能超过16个字符', trigger: 'blur'},
          ], priority: [
            {required: true, message: '请选择优先级', trigger: 'blur'},
          ], extNumber: [
            {required: true, message: '请输入IP分机号码', trigger: 'blur'},
          ],
          fleetName: [
            {required: true, message: '请选择部门名称', trigger: 'change'},
          ], System_ID: [
            {required: true, message: '请选择系统类型', trigger: 'change'},
          ], freeReleaseTime: [
            {required: true, message: '请输入空闲释放时长', trigger: 'blur'},
            {
              pattern: /^[5-9]$|^[1]\d$|^[2-9]\d$|^[1-8]\d{2}$|900/,
              message: '时长必须在5-900秒之间',
              trigger: 'blur',
            },

          ],

        },
        numberm: '',
        prioritym: '',
        freeReleaseTimem: 0,
        options: [
          {
            value: 0,
            label: '0-最高',
          }, {
            value: 1,
            label: '1',
          }, {
            value: 2,
            label: '2',
          }, {
            value: 3,
            label: '3',
          }, {
            value: 4,
            label: '4',
          }, {
            value: 5,
            label: '5',
          }, {
            value: 6,
            label: '6-最低',
          }, {
            value: 7,
            label: '7-普通工作组',
          }],
        query: '7',
        memberPriority: [
          {
            value: 0,
            label: '0-最高',
          }, {
            value: 1,
            label: '1',
          }, {
            value: 2,
            label: '2',
          }, {
            value: 3,
            label: '3',
          }, {
            value: 4,
            label: '4',
          }, {
            value: 5,
            label: '5',
          }, {
            value: 6,
            label: '6',
          }, {
            value: 7,
            label: '7-最低',
          }],
        defaultProps: {
          label: 'name',
          isLeaf: 'isLeaf',
        },
        isShowTree: true,
        addShow: false,
        showDialog: false,
        tableData1: [],
        systemId: '',
        currentAddGroup: '',     //当前群组id
        currentIndex: '',           //当前选择的列数
        currentContent: [],          //当前列信息
        flag: true,
        Nodesname: '',
        Nodesid: '',
        modifyPriority: '',
        value: 7,
        namea: '',
        numbera: '',
        freeReleaseTimea: '',
        loadTree: true,
        memberTableData: [],
        memberTableDataTableTitle: [
          {label: '名称', prop: 'name', overflowTooltip: true},
          {label: '号码', prop: 'subNumber', overflowTooltip: true},
          {label: '成员优先级', prop: 'priority', overflowTooltip: true},
          {label: '说话时长', prop: 'time', overflowTooltip: true},
        ],
        title: '',
        fleetName: '',
        fleetId: '',
        curGrpName: '',
        loadFleetTree: true,
        systemOptions: [],
        systemOptionsAll: [],
      };
    },
    created() {
      this.changeDmgPage();
    },
    mounted: function() {
      this.$nextTick(() => {
        this.queryGroupList();
        this.getSystemAll();
        this.getSystem();
      });
    },
    computed: {
      extNumberA() {
        return this.form.extNumber;
      },
      addlimitData() {
        // let data = [...this.groupData];
        return this.memberTableData;
      },
      dataWithAddPage() {
        const data = this.addlimitData;
        const start = this.addPage.pageNum * this.addPage.pageSize - this.addPage.pageSize;
        const end = start + this.addPage.pageSize;
        return [...data].slice(start, end);
      },
    },
    watch: {
      extNumberA(val) {
        if (val.length > 10) {
          this.$message.error('分机号不能超过10位数字');
          this.form.extNumber.substring(0, 10);
        }
      },
    },
    methods: {
      querySearch() {
        this.queryGroup(1);
        this.pageNum = 1;
      },
      changeDmgPage() {
        let w = window.screen.width;
        let h = window.screen.height;
        if (w == 1920) {
          if (h == 1080) {
          } else {
            this.pageSize = 12;
          }
        }
      },
      //表头样式
      headerCellClassName() {
        return 'font-size:18px;color:#515A6E;padding:10px 0px 10px 0px !important;background:#F5F5F6;border:1px solid #EBEBEB';
      },
      //表格样式
      cellStyleClassName() {
        return 'font-size:13px;text-align:center;color:#606266;padding:5px 0px 5px 0px !important;border:1px solid #EBEBEB';
      },
      loadChildrenBumen(node, resolve) {
        if (node.data) {
          this.getBumenChild(node.data.id, node.data.type, resolve);
        } else {
          setTimeout(() => { this.getBumenChild(this.FleetId, 0, resolve);}, 0);
        }
      },
      changesAddPageNum: function(val) {
        this.addPage.pageNum = val;
      },
      getBumenChild(id, type, resolve) {
        let data = {
          parentId: id,
          type: type,
        };
        UserManage.getFleet(data).then(res => {
          // 返回数据
          if (res.success) {
            var childNodes = [];
            if (res.obj) {
              childNodes = res.obj;
            }
            resolve(childNodes);
          }
        }).catch(err => {
          // 异常情况
          console.log(err);
        });

      },
      loadDataBumen: function(node) {
        console.log(node);
        this.form.fleetName = node.name;
        this.form.fleetId = node.id;
        // $('.el-select-dropdown').hide()
      },
      getSystemAll: function() {
        this.systemOptionsAll = [];
        IntercomGroupManage.getFleetSystem({fleetId: this.FleetId}).then(res => {
          if (res.success) {
            this.systemOptionsAll = res.obj.list;
            this.systemOptionsAll.unshift({System_alias: '全部', System_ID: ''});
          } else {
            // this.$message(res.msg);
          }
        }).catch(err => {
          // 异常情况
          console.log(err);
        });
      },
      getSystem: function() {
        this.systemOptions = [];
        this.systemOptionss = [];
        IntercomGroupManage.getFleetSystem({fleetId: this.FleetId}).then(res => {
          if (res.success) {
            this.systemOptions = res.obj.list;
          } else {
            this.$message(res.msg);
          }
        }).catch(err => {
          // 异常情况
          console.log(err);
        });
      },
      refreshNodeBy: function() {
        var node = this.$refs.tree.getNode(window.fleetId);
        node.loaded = false;
        node.expand();
        this.loadFleetTree = false;
        this.$nextTick(() => {
          this.loadFleetTree = true;
        });
      },
      loadData: function(node) {
        this.fleetName = node.name;
        this.fleetId = node.id;
      },
      loadDataaa: function(node) {
        var that = this;
        var data = {
          fleetId: node.id,
          pageNum: 1,
          pageSize: that.pageSize,
        };
        IntercomGroupManage.queryGroup(data).then(res => {
          var list = res.rows;
          if (res != null) {
            this.tableData = list;
            this.total = res.total;
          }
        }).catch(err => {
          // 异常情况
          console.log(err);
        });
      },
      loadChildrenn(node, resolve) {
        if (node.data) {
          this.getChildd(node.data.id, node.data.type, resolve);
        } else {
          setTimeout(() => {
            this.getChildd(this.FleetId, 0, resolve);
          }, 0);
        }
      },
      getChildd: function(id, type, resolve) {
        var data = {
          parentId: id,
          type: type,
        };
        UserManage.getFleet(data).then(res => {
          // 返回数据
          if (res.success) {
            var childNodes = [];
            if (res.obj) {
              childNodes = res.obj;
            }
            resolve(childNodes);
          }
        }).catch(err => {
          // 异常情况
          console.log(err);
        });
        /* SystemAddressBookManage.getFleet(jsonData).then(res => {
           if (res.success) {
             var childNodes = [];
             if (res.obj && res.obj.length > 0) {
               for (var i = 0; i < res.obj.length; i++) {
                 if (res.obj[i].type === 2) {
                   res.obj[i].isLeaf = true;
                 }
               }
               childNodes = res.obj;
             } else {
               var node = this.$refs.intercomtree.getNode(id);
               node.loading = false;
               return;
             }
             resolve(childNodes);
           }
         }).catch(err => {
           // 异常情况
           console.log(err);
         });*/
      },
      closeDialog() {
        this.$refs['form'].resetFields();
      },
      addGroupMember: function() {
        var that = this;
        var auditData = {
          dcId: that.$store.getters.userAccount,
          type: 5,
          status: '成功',
          describe: that.$store.getters.userName + '添加修改该组成员:' + that.curGrpName,
          time: new Date,
          fleetId: that.$store.getters.scsFleetId,
          detailType: 25,
          remark: '',
        };
        IntercomGroupManage.addGroupMemberNew({
          groupId: that.groupId,
          memberTableData: that.memberTableData,
        }).then(res => {
          if (res.success) {
            if (res.obj.count === 1) {
              this.GlobalFunc.GlaAddAudit(auditData);
              that.addShow = false;
              that.$message.success('添加成功');
              that.queryGroupList();
            } else {
              that.$message('添加失败');
            }
          } else {
            auditData.status = '失败';
            this.GlobalFunc.GlaAddAudit(auditData);

            that.$message('添加失败');
          }
        }).catch(err => {
          // 异常情况
          auditData.status = '异常';
          this.GlobalFunc.GlaAddAudit(auditData);
          console.log(err);
        });
      },
      dialogClose() {
        this.loadTree = false;
        setTimeout(() => {
          this.loadTree = true;
        }, 100);
      },
      deleteMember: function(id) {
        for (var i = 0; i < this.memberTableData.length; i++) {
          if (id === this.memberTableData[i].subId) {
            this.memberTableData.splice(i, 1);
            var node = this.$refs.tree.getNode(id);
            node.checked = false;
          }
        }
      },
      handleCheckChange: function(data, checked, indeterminate) {
        if (checked) {
          var nodeObj = {};
          nodeObj.subId = data.id;
          nodeObj.subNumber = data.number;
          nodeObj.priority = 7;
          nodeObj.time = 60;
          if (data.name.length > 10) {
            nodeObj.name = data.name.substring(0, 10) + '...';
          } else {
            nodeObj.name = data.name;
          }
          var length = false;
          for (var i = 0; i < this.memberTableData.length; i++) {
            if (data.id === this.memberTableData[i].subId) {
              length = true;
            }
          }
          if (!length) {
            this.memberTableData.push(nodeObj);
          }
        } else if (!checked) {
          for (var i = 0; i < this.memberTableData.length; i++) {
            if (data.id === this.memberTableData[i].subId) {
              this.memberTableData.splice(i, 1);
            }
          }
        }
      },
      loadChildren(node, resolve) {
        if (node.data) {
          if (node.data.type === 2) {
            var childNodes = [];
            return resolve(childNodes);
          } else {
            this.getChild(node.data.id, node.data.type, resolve);
          }
        } else {
          setTimeout(() => {
            this.getChild(this.FleetId, 0, resolve);
          }, 0);
        }

      },
      getChild: function(id, type, resolve) {
        var jsonData = {
          initID: this.FleetId,
          parentId: id,
          type: type,
          userId: this.userId,
          managePriv: 0,
          // managePriv: window.managePriv
        };
        IntercomGroupManage.getTalk(jsonData).then(res => {
          if (res.success) {
            var childNodes = [];
            if (res.obj && res.obj.length > 0) {
              for (var i = 0; i < res.obj.length; i++) {
                if (res.obj[i].type === 2) {
                  res.obj[i].isLeaf = true;
                } else if (res.obj[i].type == 1) {
                  res.obj[i].isLeaf = false;
                  res.obj[i].disabled = true;
                }
              }
              childNodes = res.obj;
            } else {
              var node = this.$refs.tree.getNode(id);
              node.loading = false;
              return;
            }
            resolve(childNodes);
          }
        }).catch(err => {
          // 异常情况
          console.log(err);
        });
        setTimeout(() => {
          for (var i = 0; i < this.memberTableData.length; i++) {
            var node = this.$refs.tree.getNode(this.memberTableData[i].subId);
            if (node != null && node.checked === false) {
              node.checked = true;
            }
          }
        }, 200);
      },
      getTreeNodes: function() {                            //确定
        var nodes = this.$refs.tree.getCheckedNodes();
        for (var i = 0; i < nodes.length; i++) {
          if (nodes[i].type == '1') {
            this.$message.error('只能添加终端');
            return;
          }
          this.Nodesname = nodes[i].name;
          this.Nodesid = nodes[i].id;
          this.flag = true;
          IntercomGroupManage.queryGroupMember({
            'userNumber': nodes[i].number,
            'groupId': this.currentAddGroup,
          }).then(res => {
            var list = res.rows;
            if (list.length != 0) {
              this.flag = false;
              this.$message.error(this.Nodesname + '已在群组中');
              return;
            }
          }).catch(err => {
            // 异常情况
            console.log(err);
          });
          setTimeout(() => {
            if (this.flag) {
              for (var i = 0; i < nodes.length; i++) {
                IntercomGroupManage.addGroupMember({
                  subscriberIds: nodes[i].id,
                  groupId: this.currentAddGroup,
                }).then(res => {
                  //更新成员表
                  var data = {'groupId': this.currentAddGroup};
                  IntercomGroupManage.queryGroupMember(data).then(res => {
                    var infos = res.rows;
                    this.tableData1 = []; //清空之前防止二次
                    infos.forEach((item) => {
                      this.tableData1.push(item);
                    });
                    // groupManageVue.simulateData();
                    // groupManageVue.isShowTree = false;
                  }).catch(err => {
                    // 异常情况
                    console.log(err);
                  });
                }).catch(err => {
                  // 异常情况
                  console.log(err);
                });
              }
            }
          }, 200);

        }
        this.$refs.tree.setCheckedKeys([]);
      },
      cancelTree: function() {                      //取消树
        this.$refs.tree.setCheckedKeys([]);
      },
      getMemberTableData: function(id) {
        SystemAddressBookManage.getMemberTableData({groupId: id}).then(res => {
          var data = res;
          if (res.success) {
            this.memberTableData = data.obj.list;
          } else {
            this.$message('');
          }
        }).catch(err => {
          // 异常情况
          console.log(err);
        });
      },

      filterNode: function(value, data) {
        if (!value) return true;
        return data.name.indexOf(value) !== -1;
      },
      queryGroupList() {
        var data = {
          fleetId: this.fleetId || this.FleetId,
          pageNum: this.pageNum,
          pageSize: this.pageSize,
        };
        IntercomGroupManage.queryGroup(data).then(res => {
          var list = res.rows;
          if (res != null) {
            /*  for (let i=0;i<list.length;i++){

              }*/
            this.tableData = list;
            this.total = res.total;
          }
        }).catch(err => {
          // 异常情况
          console.log(err);
        });
      },
      queryGroup: function(pageNum) {
        var data = {
          fleetId: this.fleetId || this.FleetId,
          name: this.name,
          number: this.number,
          extNumber: this.extNumber,
          priority: this.priority,
          systemId: this.systemId,
          pageNum: pageNum || this.pageNum,
          pageSize: this.pageSize,
        };
        IntercomGroupManage.queryGroup(data).then(res => {
          var list = res.rows;
          if (res != null) {
            this.tableData = list;
            this.total = res.total;
          }
        }).catch(err => {
          // 异常情况
          console.log(err);
        });
      },
      changePageNum: function(val) {
        this.pageNum = val;
        this.queryGroupList();
      },
      addGroup: function() {
        this.form.name = '';
        this.form.number = '';
        this.form.priority = '';
        this.form.freeReleaseTime = '';
        this.form.groupId = '';
        this.form.type = 0;
        this.form.System_ID = '';
        this.form.fleetName = '';
        this.title = '新增';
        console.log(this.form);
        IntercomGroupManage.getGroupNumber({}).then(res => {
          if (res.success) {
            this.numbera = res.obj;
            this.showDialog = true;
          } else {
            // ScsCommEvent.ScsAlertLayer(res.data.msg, false, false, false);
            return;
          }
        }).catch(err => {
          // 异常情况
          console.log(err);
        });
        this.showDialog = true;
      },

      dialogComfirm: function(formName) {
        console.log('添加');
        let validate = false;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            validate = true;
          } else {
            validate = false;
            return false;
          }
        });
        if (validate === false) {
          return;
        }
        if (this.form.type === 0) {

          var data = {
            name: this.form.name,
            // fleetId: this.FleetId,
            number: this.form.number,
            priority: this.form.priority,
            freeReleaseTime: this.form.freeReleaseTime,
            userId: this.UserAccount,
            systemId: this.form.System_ID,
            fleetId: this.form.fleetId,
            sipNumber: this.form.extNumber,
            channelId: 1,
            // versionType:1,
          };
          if (data.sipNumber.length > 20) {
            this.$message.error('IP分机号码长度不能超过20个字符！');
            return;
          }

          var auditData = {
            dcId: this.$store.getters.userAccount,
            type: 5,
            status: '成功',
            describe: this.$store.getters.userName + '添加群组:' + data.name,
            time: new Date,
            fleetId: this.$store.getters.scsFleetId,
            detailType: 25,
            remark: '',
          };
          console.log(data);
          IntercomGroupManage.addGroupByDC(data).then(res => {
            console.log('好好干哈res');
            console.log(res);
            if (res.success) {
              console.log('添加成功');
              let row = res.obj.count;
              if (row) {
                switch (row) {
                  case 1 ://添加修改成功
                    this.GlobalFunc.GlaAddAudit(auditData);
                    this.$message.success('添加成功');
                    break;
                  case 2 ://分机号已被使用
                    auditData.status = '失败:分机号已被使用';
                    this.GlobalFunc.GlaAddAudit(auditData);
                    this.$message.error('分机号已被使用');
                    break;
                  case 3 ://分机号类型错误
                    auditData.status = '失败:分机号类型错误';
                    this.GlobalFunc.GlaAddAudit(auditData);
                    this.$message.error('分机号类型错误');
                    break;
                  case 4 ://非分机号
                    auditData.status = '失败:群组名称或群组号码重复';
                    this.GlobalFunc.GlaAddAudit(auditData);
                    this.$message.error('群组名称或群组号码重复');
                    break;
                  default:
                    auditData.status = '失败';
                    this.GlobalFunc.GlaAddAudit(auditData);
                    break;
                }
              }
              this.showDialog = false;
              this.tableData = []; //清空之前防止二次
              this.queryGroupList();
            } else {
              auditData.status = '失败';
              this.GlobalFunc.GlaAddAudit(auditData);
              this.$message(res.msg);
              this.showDialog = false;
              this.tableData = []; //清空之前防止二次
              this.queryGroupList();
            }
          }).catch(err => {
            // 异常情况
            auditData.status = '异常';
            this.GlobalFunc.GlaAddAudit(auditData);
            console.log(err);
          });
        } else {
          var data = {
            groupId: this.form.groupId,
            name: this.form.name,
            number: this.form.number,
            priority: this.form.priority,
            freeReleaseTime: this.form.freeReleaseTime,
            systemId: this.form.System_ID,
            fleetId: this.form.fleetId,
            sipNumber: this.form.extNumber,
            channelId: 1,
            // versionType:1,
          };
          if (data.sipNumber.length > 20) {
            this.$message.error('IP分机号码长度不能超过20个字符！');
            return;
          }
          var auditData = {
            dcId: this.$store.getters.userAccount,
            type: 5,
            status: '成功',
            describe: this.$store.getters.userName + '修改群组:' + data.name,
            time: new Date,
            fleetId: this.$store.getters.scsFleetId,
            detailType: 25,
            remark: '',
          };
          IntercomGroupManage.updateGroup(data).then(res => {
            if (res.success) {
              let row = res.obj.count;
              if (row) {
                switch (row) {
                  case 1 ://添加修改成功
                    this.GlobalFunc.GlaAddAudit(auditData);
                    this.$message.success('修改成功');
                    break;
                  case 2 ://分机号已被使用
                    auditData.status = '失败:分机号已被使用';
                    this.GlobalFunc.GlaAddAudit(auditData);
                    this.$message.error('分机号已被使用');
                    break;
                  case 3 ://分机号类型错误
                    auditData.status = '失败:分机号类型错误';
                    this.GlobalFunc.GlaAddAudit(auditData);
                    this.$message.error('分机号类型错误');
                    break;
                  case 4 ://非分机号
                    auditData.status = '失败:群组名称或群组号码重复';
                    this.GlobalFunc.GlaAddAudit(auditData);
                    this.$message.error('群组名称或群组号码重复');
                    break;
                  default:
                    auditData.status = '失败';
                    this.GlobalFunc.GlaAddAudit(auditData);
                    break;
                }
              }
              this.showDialog = false;
              this.queryGroupList();
            } else {
              auditData.status = '失败';
              this.GlobalFunc.GlaAddAudit(auditData);
              this.$message('修改失败');
            }
          }).catch(err => {
            // 异常情况
            auditData.status = '异常';
            this.GlobalFunc.GlaAddAudit(auditData);
            console.log(err);
          });
        }

      },
      deleteGroup: function(row, table) {
        var that = this;
        var auditData = {
          dcId: this.$store.getters.userAccount,
          type: 5,
          status: '成功',
          describe: this.$store.getters.userName + '删除群组:' + table[row].name,
          time: new Date,
          fleetId: this.$store.getters.scsFleetId,
          detailType: 25,
          remark: '',
        };

        this.$confirm('是否确定删除', '', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          var data = {};
          var groupId = table[row].groupId;
          /*    axios.post('/scs/group/deleteGroup?groupId=' + groupId + '', data)
              .then(function (res) {

              })
              .catch(function (err) {
                that.$message('删除失败');
              });*/
          IntercomGroupManage.deleteGroup(
              {
                groupId: groupId,
                versionType: 1,
                // channelId:1,
              },
          ).then(res => {
            if (res.success) {
              this.GlobalFunc.GlaAddAudit(auditData);

              table.splice(row, 1);
              that.$message.success('删除成功');
              const totalPage = Math.ceil((that.total - 1) / that.pageSize);
              //记住删除最后一条数据时当前码是最后一页
              const tempPageNum = that.pageNum > totalPage ? totalPage : that.pageNum;
              //实现跳转
              that.pageNum = tempPageNum < 1 ? 1 : tempPageNum;
              that.queryGroup();
//                                    ScsCommEvent.ScsAlertLayer("群组已删除", false, false, false);
//               ScsTree.refreshTree()
            } else {
              auditData.status = '失败';
              this.GlobalFunc.GlaAddAudit(auditData);

              that.$message('删除失败');
            }
          }).catch(err => {
            // 异常情况
            auditData.status = '异常';
            this.GlobalFunc.GlaAddAudit(auditData);
            console.log(err);
          });

        });

      },
      modifyGroup(index, rows) {
        console.log(rows);
        this.form.name = rows[index].name;
        this.form.number = rows[index].number;
        this.form.priority = rows[index].priority;
        this.form.freeReleaseTime = rows[index].freeReleaseTime;
        this.form.System_ID = rows[index].systemId;
        this.form.groupId = rows[index].groupId;
        this.form.type = 1;
        this.form.fleetName = rows[index].fleetName;
        this.form.fleetId = rows[index].fleetId;
        this.form.extNumber = rows[index].sipNumber;
        this.title = '修改';
        this.$nextTick(() => {
          this.showDialog = true;
        });
        this.currentIndex = index;
        this.currentContent = rows[index];
      },

      memberManger: function(row) {
        console.log(row);
        this.addShow = true;
        this.title = '成员管理';
        console.info('%c 成员管理' + row.name, 'color:green');
        this.curGrpName = row.name;
        this.groupId = row.groupId;
        var data = {
          groupId: row.groupId,
        };
        IntercomGroupManage.queryGroupMember(data).then(res => {
          this.memberTableData = res.rows;
        }).catch(err => {
          // 异常情况
          console.log(err);
        });
        // this.getMemberTableData(row.groupId);
      },
      deleteRow: function(index, rows) {

        ScsCommEvent.ScsAlertLayer('确认删除？', false, true, closeChannel3, null, null);

        function closeChannel3() {
          var data = {
            subscriberId: rows[index].subId,
            groupId: rows[index].groupId,
          };
          axios.post('/scs/group/deleteGroupMember', data).then(function(res) {
            if (res.status == '200') {
              rows.splice(index, 1);
              ScsCommEvent.ScsAlertLayer('删除成功', false, false, false);
            }
          }).catch(function(err) {
            console.warn(err);
          });
          IntercomGroupManage.updateSystemAddressBookIndex(data).then(res => {

          }).catch(err => {
            // 异常情况
            console.log(err);
          });
        }
      },

      modifyMemberInfo: function(index, rowInfo) {
        if (rowInfo.subId == undefined) {
          return;
        }
        var data = {
          groupId: rowInfo.groupId,
          subscriberId: rowInfo.subId,
          priority: rowInfo.priority,
          speakTime: rowInfo.time,
        };
        IntercomGroupManage.updateGroupMember(data).then(res => {
          if (res.status == '200') {
            ScsCommEvent.ScsAlertLayer('成功', false, false, false);
          }
        }).catch(err => {
          // 异常情况
          console.log(err);
        });
      },
      CloseMemberList: function() {
        this.addShow = false;
      },
    },
  };
</script>

<style scoped>
    /deep/ .el-checkbox__input > .el-checkbox__inner {
        /*display: none;*/
    }

    /deep/ .is-leaf + .el-checkbox .el-checkbox__inner {
        display: inline-block;
    }

    /deep/ .el-form-item__label {
        font-size: 22px !important;
    }

    /deep/ .el-input__inner {
        font-size: 22px !important;
    }

    /deep/ .el-tree-node {
        margin-top: 25px !important;
        height: auto;
    }


    /deep/ .el-input__inner {
        font-size: 22px;
    }

    /deep/ .el-dialog__title {
        font-size: 22px;
    }

    /deep/ .el-icon-close:before {
        font-size: 25px;
    }

   /deep/ .el-icon-caret-right:before {
        font-size: 40px!important;
    }

    /*分页修改开始*/
    .el-pagination /deep/ .el-pagination__total {
        font-size: 20px;
        line-height: 60px;
    }

    .el-pagination /deep/ button /deep/ .el-icon .el-icon .el-icon-arrow-left {
        font-size: 50px;
        height: 60px;
        width: 40px;
    }

    /deep/ .el-pager li {
        margin-top: 15px;
        height: 60px;
        width: 60px;
        line-height: 60px;
        font-size: 18px;
    }

    /deep/ .el-pagination.is-background .btn-prev, .el-pager li {
        height: 60px;
        width: 60px;
    }

    /deep/ .el-pagination.is-background .btn-next, .el-pager li {
        height: 60px;
        width: 60px;
    }

    /deep/ .el-pagination .btn-prev .el-icon {
        font-size: 40px;
    }

    /deep/ .el-pagination .btn-next .el-icon {
        font-size: 40px;
    }

    /deep/ .el-checkbox {
        height: 30px;
        width: 30px;
    }

    /deep/ .el-checkbox__inner {
        width: 30px;
        height: 30px;
    }

    /deep/ .el-checkbox /deep/ .el-checkbox__inner {
        height: 30px;
        width: 30px;
    }

    /deep/ .el-checkbox /deep/ .el-checkbox__input {
        vertical-align: middle !important;
    }

    /deep/ .el-checkbox /deep/ .el-checkbox__inner:before {
        font-size: 20px;
        margin: 3px auto auto 3px;
        text-align: center;
        line-height: 40px;
    }

    /deep/ .el-checkbox /deep/ .el-checkbox__inner:after {
        height: 12px;
        width: 12px;
        font-size: 20px;
        margin: 3px auto auto 3px;
        text-align: center;
        /*line-height: 40px;*/
    }

    /deep/ .el-tree-node__content {
        height: 40px !important;
    }

    /*分页修改结束*/

    .searchFormBtn {
        margin: 5px;
    }

    .groupMan_groupSearchNumber {
        margin-left: 5px;
    }

    .treeLableImg {
        height: 25px;
        width: 25px;
        margin-right: 10px;
        vertical-align: middle
    }

    .leftTreeClass {
        float: left;
        width: 19%;
        height: 600px;
        background: white;
        position: absolute;
    }

    /*优化start*/

    @media screen and (max-width: 1920px) and (max-height: 1280px) {
        .leftTreeClass {
            float: left;
            width: 19%;
            height: 1200px;
            background: #ffffff;
            position: absolute;
            border-right: 1px solid #EBEEF5;
        }
    }

    /*优化end*/
    .groupMgStyleBigBoxLeft {
        float: left;
        width: 30%;
        height: 450px;
    }

    .groupMgStyleBigBox {
        float: right;
        width: 67%;
        height: 450px;
        background: ghostwhite;
        overflow-y: auto;
        overflow-x: hidden;

    }

</style>
<style>

</style>
